package com.sugon.controller.device;

import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.dao.SpecialDevCheckRecordDao;
import com.sugon.entity.SpecialDevCheckRecordEntity;
import com.sugon.service.SpecialDevCheckRecordService;
import com.sugon.service.SysMsgService;
import com.sugon.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 特种设备校验台账单Controller
 *
 * @author sugon
 * @date 2020-12-17 14:16:21
 */
@Controller
@RequestMapping("specialdevcheckrecord")
public class SpecialDevCheckRecordController {

    @Autowired
    private SpecialDevCheckRecordService specialDevCheckRecordService;
    @Autowired
    private SysMsgService msgService;
    @Autowired
    private SpecialDevCheckRecordDao specialDevCheckRecordDao;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("specialdevcheckrecord:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<SpecialDevCheckRecordEntity> specialDevCheckRecordList = specialDevCheckRecordService.queryList(query);
        int total = specialDevCheckRecordService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(specialDevCheckRecordList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("specialdevcheckrecord:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        SpecialDevCheckRecordEntity specialDevCheckRecord = specialDevCheckRecordService.queryObject(id);

        return R.ok().put("specialDevCheckRecord", specialDevCheckRecord);
    }

    /**
     * 保存
     */
    @AutoLog("特种设备校验台账单--保存")
    @RequestMapping("/save")
//    @RequiresPermissions("specialdevcheckrecord:save")
    @ResponseBody
    public R save(@RequestBody SpecialDevCheckRecordEntity specialDevCheckRecord) {
        // 常规校验
        String certificateNumber = specialDevCheckRecord.getCertificateNumber();
        if (StrUtil.isEmpty(certificateNumber)) {
            return R.error("特种设备校验台账必选！");
        }
        specialDevCheckRecord.setRecorder(ShiroUtils.getUserId());
        boolean flag = specialDevCheckRecordService.save(specialDevCheckRecord) > 0;
        if (flag) {
            // 1、删除消息中心所有该记录相关消息
            msgService.delSysMsgService(specialDevCheckRecord.getId(), ProcessInfoEnum.SPECDEV_CHECK_REMIND.name(), null);
            // 2、修改对应特种设备的最近检验日期和证书编号
            specialDevCheckRecordDao.updateSpecDevCheckRecord("SPECIAL_DEV_" + specialDevCheckRecord.getDevType(), specialDevCheckRecord.getDevId(), specialDevCheckRecord.getCheckDate(), certificateNumber, specialDevCheckRecord.getCheckDate());
        }
        return flag ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @AutoLog("特种设备校验台账单--修改")
    @RequestMapping("/update")
    @RequiresPermissions("devspeccheck:update")
    @ResponseBody
    public R update(@RequestBody SpecialDevCheckRecordEntity specialDevCheckRecord) {
        boolean flag = specialDevCheckRecordService.update(specialDevCheckRecord) > 0;
        if (flag) {
            // 1、删除消息中心所有该记录相关消息
            msgService.delSysMsgService(specialDevCheckRecord.getId(), ProcessInfoEnum.SPECDEV_CHECK_REMIND.name(), null);
            // 2、修改对应特种设备的最近检验日期和证书编号
            specialDevCheckRecordDao.updateSpecDevCheckRecord("SPECIAL_DEV_" + specialDevCheckRecord.getDevType(), specialDevCheckRecord.getDevId(), specialDevCheckRecord.getCheckDate(), specialDevCheckRecord.getCertificateNumber(), specialDevCheckRecord.getCheckDate());
        }
        return specialDevCheckRecordService.update(specialDevCheckRecord) > 0 ? R.ok() : R.error();
    }

    /**
     * 删除
     */
    @AutoLog("特种设备校验台账单--删除")
    @RequestMapping("/delete")
//    @RequiresPermissions("specialdevcheckrecord:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        specialDevCheckRecordService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<SpecialDevCheckRecordEntity> list = specialDevCheckRecordService.queryList(params);

        return R.ok().put("list", list);
    }
}
